package com.gitee.wsl.ext.bitcode

/**
 * Encodes the Byte in zigzag pattern so negative values are
 * able to encode much more efficiently into varInt
 */
 fun Byte.encodeZigZag() = this.toInt().encodeZigZag().toByte()

/** Decodes the Short out of zigzag pattern so bytes have the normal native order again */
 fun Byte.decodeZigZag() = (this.toInt() and 0xFF).decodeZigZag().toByte()

/**
 * Encodes the Short in zigzag pattern so negative values are
 * able to encode much more efficiently into varInt
 */
 fun Short.encodeZigZag() = this.toInt().encodeZigZag().toShort()

/** Decodes the Short out of zigzag pattern so bytes have the normal native order again */
 fun Short.decodeZigZag() = (this.toInt() and 0xFFFF).decodeZigZag().toShort()

/**
 * Encodes the Int in zigzag pattern so negative values are
 * able to encode much more efficiently into varInt
 */
 fun Int.encodeZigZag() = this shl 1 xor (this shr 31)

/** Decodes the Int out of zigzag pattern so bytes have the normal native order again */
 fun Int.decodeZigZag() = this ushr 1 xor -(this and 1)


/**
 * Encodes the Long in zigzag pattern so negative values are
 * able to encode much more efficiently into varInt
 */
 fun Long.encodeZigZag() = this shl 1 xor (this shr 63)

/** Decodes the Long out of zigzag pattern so bytes have the normal native order again */
 fun Long.decodeZigZag() = this ushr 1 xor -(this and 1)

